package com.yu.yupicture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.yupicture.manger.upload.CosUntil;
import com.yu.yupicture.Untils.ThrowUntil;
import com.yu.yupicture.annotation.AuthCheck;
import com.yu.yupicture.common.BaseReponse;
import com.yu.yupicture.common.DeleteRequest;
import com.yu.yupicture.common.Result;
import com.yu.yupicture.constans.UserConstans;
import com.yu.yupicture.exception.ErrorCode;
import com.yu.yupicture.exception.biusnessException;
import com.yu.yupicture.model.domain.dto.user.*;
import com.yu.yupicture.model.domain.entry.User;
import com.yu.yupicture.model.domain.vo.UserLoginVo;
import com.yu.yupicture.model.domain.vo.UserVo;
import com.yu.yupicture.model.enums.UserRoleEnums;
import com.yu.yupicture.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Api(tags = "用户接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
    private final CosUntil cosUntil;

    private final UserService userService;

    /**
     * 用户注册
     * @param userRegistDto
     * @return
     */

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseReponse<Long> userRegister(@RequestBody userRegistDto userRegistDto) {
        ThrowUntil.throwIf(userRegistDto == null, ErrorCode.PARAMS_ERROR);
        Long userId = userService.userRegister(userRegistDto);
        return Result.success(userId);
    }


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseReponse<UserLoginVo> userLogin(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request) {
        ThrowUntil.throwIf(userLoginDto == null, ErrorCode.PARAMS_ERROR);
        UserLoginVo userLoginVo = userService.userLogin(userLoginDto,request);
        return Result.success(userLoginVo);
    }

    @ApiOperation("获取当前登录用户")
    @GetMapping("/get/login")
    public BaseReponse<UserLoginVo> getLoginUser(HttpServletRequest request) {
        User user= userService.getLoginUser(request);
        UserLoginVo userLoginVo = userService.getSafeUser(user);
        return Result.success(userLoginVo);
    }


    /**
     * 用户登出
     * @param request
     * @return
     */
    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public BaseReponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUntil.throwIf(request == null,ErrorCode.PARAMS_ERROR);
        boolean result = userService.logout(request);
        return Result.success(result);
    }

    /**
     * 创建用户
     * @param userAddRequest
     * @return
     */
    @AuthCheck(value = UserConstans.ADMIN)
    @ApiOperation("创建用户")
    @PostMapping("/add")
    public BaseReponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUntil.throwIf(userAddRequest == null,ErrorCode.PARAMS_ERROR);
        Long userId = userService.addUser(userAddRequest);
        return Result.success(userId);
    }

    /**
     * 删除用户
     * @param deleteRequest
     * @return
     */
    @AuthCheck(value = UserConstans.ADMIN)
    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public BaseReponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        ThrowUntil.throwIf(deleteRequest == null,ErrorCode.PARAMS_ERROR);
        boolean result = userService.deleteUser(deleteRequest);
        return Result.success(result);
    }

    /**
     * 根据id获取用户(脱敏)
     * @param id
     * @return
     */
    @ApiOperation("根据id获取用户")
    @GetMapping("/get/vo")
    public BaseReponse<UserVo> getUserVo(Long id) {
        ThrowUntil.throwIf(id == null,ErrorCode.PARAMS_ERROR);
        UserVo userVo = userService.getUserVo(id);
        return Result.success(userVo);
    }

    /**
     * 根据id获取用户(未脱敏)
     * @param id
     * @return
     */
    @AuthCheck(value = UserConstans.ADMIN)
    @ApiOperation("根据id获取用户")
    @GetMapping("/get")
    public BaseReponse<User> getUser(Long id) {
        ThrowUntil.throwIf(id == null,ErrorCode.PARAMS_ERROR);
        User user = userService.getUser(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * @param userUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新用户")
    public BaseReponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        if (!user.getUserRole().equals(UserRoleEnums.ADMIN.getValue()) && !user.getId().equals(userUpdateRequest.getId())) {
            throw new biusnessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean result = userService.updateUser(userUpdateRequest);
        return Result.success(result);
    }

    /**
     * 分页查询用户信息（管理端）
     * @param userQueryRequest
     * @return
     */
    @AuthCheck(value = UserConstans.ADMIN)
    @PostMapping("/list/page/vo")
    @ApiOperation("分页查询用户信息（管理端）")
    public BaseReponse<Page<UserVo>> listUserVoByPage(@RequestBody UserQueryRequest userQueryRequest) {
     ThrowUntil.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Page<UserVo> userVoPage = userService.listUserVoByPage(userQueryRequest);
        return Result.success(userVoPage);
    }

    /**
     * 更新用户头像
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/update/avatar")
    public BaseReponse<String> updateUserAvatar(
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request) {

        // 4. 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        ThrowUntil.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        String key= "/updateUserAvatar/" + loginUser.getId();

        // 5. 上传文件并获取URL
        return Result.success(cosUntil.userUpdateAva(key, file));

    }


}
